﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Collections;
using System.Media;
using System.Windows.Media.Animation;

namespace RhythmDetect
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer1 = new DispatcherTimer();
        SoundPlayer beatPlayer = new SoundPlayer(RhythmDetect.Properties.Resources.rhythm);
        int pressNum = -1;
        bool playSwitch=false;
        bool detectSwitch = false;
        TimeSpan tsa;
        ArrayList arr = new ArrayList();
        Storyboard Beating;
        Storyboard Detect;

        Storyboard Snow1;
        Storyboard Snow2;
        Storyboard Snow3;

        public MainWindow()
        {
            InitializeComponent();
            Beating = (Storyboard)Resources["Beating"];
            Detect = (Storyboard)Resources["Detect"];
            Snow1 = (Storyboard)Resources["Snow1"];
            Snow2 = (Storyboard)Resources["Snow2"];
            Snow3 = (Storyboard)Resources["Snow3"];
            timer1.Interval = new TimeSpan(0, 0, 0, 0,2);
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();

            imgMiku.MouseLeftButtonDown += new MouseButtonEventHandler(imgMiku_MouseLeftButtonDown);
            this.btnExit.Click += new RoutedEventHandler(btnExit_Click);
            this.btnAbout.Click+=new RoutedEventHandler(btnAbout_Click);
            GridBg.MouseLeftButtonDown += new MouseButtonEventHandler(GridBg_MouseLeftButtonDown);
            this.imgMikuBack.MouseLeftButtonDown += new MouseButtonEventHandler(imgMiku_MouseLeftButtonDown);
            this.rtgDetect.MouseLeftButtonDown += new MouseButtonEventHandler(rtgDetect_MouseLeftButtonDown);
        
        }

        void rtgDetect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            detect();
        }

        void GridBg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }


        void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void imgMiku_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (playSwitch == false && tbSetBeats.Text != "")
            {
                beatPlay();
            }
            else
            {
                beatStop();
            }
        }
        //时间控件;
        DateTime tsd1;
        int count = 0;
        int mikuCount = 1;
        void timer1_Tick(object sender, EventArgs e)
        {
            
            if (detectSwitch)
            {
                TimeSpan tsThis = DateTime.Now - (DateTime)arr[0];
                lbTime.Text = "本次时间:" + tsThis.ToString();
                
                if ((DateTime.Now - (DateTime)arr[pressNum]).TotalSeconds > 3)
                {
                    stopDetect();
                }
                else if (pressNum > 0)
                {
                    if ((DateTime.Now - (DateTime)arr[pressNum]).TotalMilliseconds > (tsa.TotalMilliseconds / (arr.Count - 1)) * 2)
                    {
                        stopDetect();
                    }
                }
            }
            
            if (playSwitch)
            {
                if (count==0)
                {
                    tsd1 = DateTime.Now;
                    count++;
                }
                
                if ((DateTime.Now - tsd1).TotalMilliseconds >= (60000 / Convert.ToDouble(tbSetBeats.Text)*count))
                {
                    count++;
                    beatPlayer.Play();
                    switch(count%3)
                    {
                        case 0:
                            Snow1.Begin();break;
                        case 1:
                            Snow2.Begin();break;
                        case 2:
                            Snow3.Begin();break;
                        default: break;
                    }
                    Beating.Begin();

                }

                if (mikuCount == 0)
                {
                    mikuCount++;
                }

                //控制MIKU头像摇摆
                if ((DateTime.Now - tsd1).TotalMilliseconds >= (60000 / Convert.ToDouble(tbSetBeats.Text) * (mikuCount)/4))
                {
                    imgMiku.Source = new BitmapImage(new Uri("Images/Miku2/"+((mikuCount+2)%8+1).ToString()+".png",UriKind.Relative));
                    if ((mikuCount) % 8 == 0 || (mikuCount % 8)== 4)
                    imgMikuBack.Source = new BitmapImage(new Uri("Images/Miku2/" + ((mikuCount + 2) % 8 + 1).ToString() + ".png", UriKind.Relative));
                    
                    mikuCount++;
                    
                }

                lbTime2.Text = "次数：" + count.ToString() + " 时间：" + (DateTime.Now - tsd1).ToString();
                
            }
        }
        //按下按键
        public void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F || e.Key == Key.J)
            {
                detect();
            }
            else if (e.Key == Key.Space)
            {
                beatPlay();
            }
        }

        void detect()
        {
            if (playSwitch == true)
            {
                beatStop();

            }
            if (detectSwitch == false) detectSwitch = true;
            pressNum++;
            beatPlayer.Play();
            arr.Add(DateTime.Now);
            if (pressNum > 0)
            {
                TimeSpan ts = (DateTime)arr[pressNum] - (DateTime)arr[pressNum - 1];
                lbBeatSpeed.Text = "采样次数:" + arr.Count.ToString() + "  本次间隔: " + ts.TotalMilliseconds.ToString() + " ms";
                tsa = (DateTime)arr[pressNum] - (DateTime)arr[0];
                if (pressNum > 1)
                {
                    double beats = (60000 / (((DateTime)arr[pressNum] - (DateTime)arr[1]).TotalMilliseconds / (arr.Count - 2)));
                    lbTime3.Text = "BPM: " + beats.ToString("#.#") + "拍/分";
                    tbSetBeats.Text = beats.ToString("#.");
                }

            }

            Detect.Begin();
        }

        private void btnAbout_Click(object sender, RoutedEventArgs e)
        {
            AboutHelp aboutHelp = new AboutHelp();
            aboutHelp.ShowDialog();
        }

        
    }
}
